/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <algorithm>

#include "CommChannel.h"
#include "Session.h"
#include "StringBuilder.h"
#include "Error.h"
#include "color.h"

using namespace Chronos;
using namespace std;

#define NAME '[' << color << name << ANSI_NORMAL << ']'

#define CHECK_IF_MEMBER \
    if (iter == members.end()) { \
        session.write() << "You are not a member of channel " << NAME << ".\n"; \
        return; \
    }

CommChannel::CommChannel(const string& name_, const char* color_, bool permanent_)
    : name(name_)
	, color(color_)
    , permanent(permanent_)
{}

void CommChannel::addMember(Session& session) {
    MembersIter iter = find(members.begin(), members.end(), &session);
	if (iter != members.end()) {
        session.write() << "You are already member of channel " << NAME << ".\n";
        return;
	}
	session.write() << "You joined the " << NAME << " channel.\n";
	broadcast(session.getName() + " joins the channel.");
	members.push_back(&session);
}


void CommChannel::removeMember(Session& session) {
    MembersIter iter = find(members.begin(), members.end(), &session);
    CHECK_IF_MEMBER;
    members.erase(iter);
	session.write() << "You left the " << NAME << " channel.\n";
    broadcast(session.getName() + " has left the channel.");
}


void CommChannel::removeIfMember(Session& session) {
    MembersIter iter = find(members.begin(), members.end(), &session);
    if (iter != members.end()) removeMember(session);
}


const string& CommChannel::getName() const { return name; }

const string CommChannel::getFullName() const {
    StringBuilder buffer;
    buffer << NAME;
    return string(buffer);
}

bool CommChannel::isEmpty() const {
    return (members.size() == 0);
}

bool CommChannel::isPermanent() const { return permanent; }


void CommChannel::writeName(Session& session) const {
    session.write() << "   " << name;
    MembersConstIter iter = find(members.begin(), members.end(), &session);
    if (!permanent) session.write() << " P";
    if (iter != members.end()) session.write() << " *";
    session.write() << '\n';
}


void CommChannel::showMembers(Session& session) const {
    MembersConstIter iter = find(members.begin(), members.end(), &session);
    CHECK_IF_MEMBER;
    session.write() << NAME << " members: ";
    bool first = true;
    for (iter = members.begin(); iter != members.end(); ++iter) {
        if (first) first = false;
        else session.write() << ", ";
        session.write() << (*iter)->getName();
    }
    session.write() << '\n';
}


void CommChannel::write(Session& session, const string& msg) {
    MembersConstIter iter = find(members.begin(), members.end(), &session);
    CHECK_IF_MEMBER;
	broadcast(session.getName() + ": " + msg);
}


void CommChannel::broadcast(const string& text) {
    StringBuilder buffer;
    buffer << color << '[' << name << "] " << text << ANSI_NORMAL << '\n';
	for (MembersConstIter iter = members.begin(); iter != members.end(); ++iter) {
		(*iter)->write() << buffer;
	}
}


